var orderList = {};
var cancelVerification = {};
var OrderList = function () {
    this.dictMap = {}; //数据字典
    this.rowData; //选中行数据
    //初始化信息
    this.init = function () {
        var paramMap = layuiFunciton.getURLParamMap();
        if (paramMap) {
            $("#tranType").val(paramMap["tranType"])
        }
        //初始化数据字典
        layuiFunciton.ajaxRequest({
            url: layuiFunciton.URL + '/sys/dictVal/getByTypeCode',
            method: 'get',
            maskState: false,
            success: function (data) {
                data.forEach(function (obj, index) {
                    orderList.dictMap[obj.valCode] = obj.valName;
                });
            }
        });
        //初始化搜索条件 日期范围组件
        layuiFunciton.layTime({
            elem: '#orderDateStart'
            , format: 'yyyy-MM-dd'
        });
        layuiFunciton.layTime({
            elem: '#orderDateEnd'
            , format: 'yyyy-MM-dd'
        });
        layuiFunciton.layTime({
            elem: '#dateShipmentStart'
            , format: 'yyyy-MM-dd'
        });
        layuiFunciton.layTime({
            elem: '#dateShipmentEnd'
            , format: 'yyyy-MM-dd'
        });
        //初始化搜索条件业务员
        layuiFunciton.ajaxRequest({
            url: layuiFunciton.URL + '/sysUser/findUserList',
            data: {page: 1, limit: 99999},
            maskState: false,         //是否开启遮罩
            success: function (data) {
                var optionHtml = '<option value="">请选择</option>';
                data.forEach(function (obj, index) {
                    optionHtml += '<option value="' + obj.name + '">' + obj.name + '</option>';
                });
                $("#salesman").html(optionHtml);
            }
        });
        //初始化数据字典
        var businessTypes = "YWLX_T";
        if (paramMap) {
            $("#tranType").val(paramMap.tranType);
            if (paramMap["tranType"] == "1") {//铁路运输
                businessTypes = "YWLX_T";
                //发出站
                layuiFunciton.selectDict("aStation", "ZD");
                //到站
                layuiFunciton.selectDict("arriveStation", "ZD");
            } else if (paramMap["tranType"] == "2") {//水路运输
                businessTypes = "YWLX_S";
                $("#goodsWere").text("提单号");
                $("#aStationText").text("发出港");
                $("#arriveStationText").text("到港");
                //发出港
                layuiFunciton.selectDict("aStation", "GK");
                //到港
                layuiFunciton.selectDict("arriveStation", "GK");
            } else if (paramMap["tranType"] == "3") {//公路运输
                //发出站
                layuiFunciton.selectDict("aStation", "ZD");
                //到站
                layuiFunciton.selectDict("arriveStation", "ZD");
                businessTypes = "YWLX_G";
            }
        }
        //业务类型
        layuiFunciton.selectDict("businessTypes", businessTypes);
        //品名
        layuiFunciton.selectDict("ofTheGoods", "Of_the_goods");
        //箱型
        layuiFunciton.selectDict("aBox", "XX");
        //发货单位
        layuiFunciton.selectDict("deliveryUnit", "FHDW");
        //收货人
        //layuiFunciton.selectDict("receivingUnit", "SHDW");
        //结算人
        layuiFunciton.selectDict("settlementPeople", "JSR");
        //装货地点
        layuiFunciton.selectDict("standby1", "ZHDD");
    };
    this.trim = function (str) {
        return str.replace(/(^\s*)|(\s*$)/g, "");
    };
    //初始化表格
    this.initTalbe = function () {
        layuiFunciton.tableInit({
            toolbar: '.btn',
            tableId: 'orderListTableId',
            url: layuiFunciton.URL + '/wb/sterilisation/records/findReceivables',
            id: 'orderListId',
            totalRow: true,
            searchFromId: 'searchForm',
            tableFilter: 'orderListFilter',  //表格监听名称,
            cols: [[
                , {field: 'orderDate', title: '下单日期', sort: true}
                , {field: 'orderCode', title: '订单编号', sort: true}
                , {field: 'dateShipment', title: '发运日期', sort: true, totalRowText: '合计：'}
                , {
                    field: 'businessTypes', title: '业务类型', sort: true,
                    templet: function (row) {
                        return orderList.dictMap[row.businessTypes];
                    }
                }
                , {
                    field: 'ofTheGoods', title: '品名', sort: true,
                    templet: function (row) {
                        if(orderList.dictMap[row.ofTheGoods]){
                            return orderList.dictMap[row.ofTheGoods];
                        }else{
                            return "";
                        }
                    }
                }
                , {
                    field: 'aBox', title: '箱型', sort: true,
                    templet: function (row) {
                        return orderList.dictMap[row.aBox];
                    }
                }
                , {field: 'cartonNo', title: '箱号', sort: true}
                , {
                    field: 'deliveryUnit', title: '发货单位', sort: true,
                    templet: function (row) {
                        if (orderList.dictMap[row.deliveryUnit]) {
                            return orderList.dictMap[row.deliveryUnit];
                        } else {
                            return "";
                        }
                    }
                }, {
                    field: 'standby1', title: '装货地点', sort: true,
                    templet: function (row) {
                        if (orderList.dictMap[row.standby1]) {
                            return orderList.dictMap[row.standby1];
                        } else {
                            return "";
                        }
                    }
                }
                , {
                    field: 'receivingUnit', title: '收货人', sort: true/*,
                     templet: function (row) {
                     return orderList.dictMap[row.receivingUnit];
                     }*/
                }
                , {
                    field: 'settlementPeople', title: '结算人', sort: true,
                    templet: function (row) {
                        return orderList.dictMap[row.settlementPeople];
                    }
                }
                , {field: 'goodsWere', title: '货票号', sort: true}
                , {field: 'chargeableWeight', title: '计费重量', sort: true}
                , {
                    field: 'accountsReceivableAmount', title: '应收金额', sort: true,
                    totalRowText: '<span style="color: green" class="accountsReceivableAmount">0.00</span>',
                    templet: function (row) {
                        return Number(row.accountsReceivableAmount).toFixed(2);
                    }
                }, {
                    field: 'sterilisationAmount', title: '已收款', sort: true,
                    totalRowText: '<span style="color: green" class="sterilisationAmount">0.00</span>',
                    templet: function (row) {
                        return Number(row.sterilisationAmount).toFixed(2);
                    }
                }, {
                    field: '', title: '欠款', sort: true,
                    totalRowText: '<span style="color: green" class="qian">0.00</span>',
                    templet: function (row) {
                        return (Number(row.accountsReceivableAmount) - Number(row.sterilisationAmount)).toFixed(2);
                    }
                }
                , {
                    title: '操作',
                    width: 80,
                    align: "center",
                    fixed: 'right',
                    templet: function (row) {
                        return '<a href="javascript:void(0)" style="color:#009feb" lay-event="detail">明细</a>';
                    }
                }
            ]],
            returnData: function (res) {
                var accountsReceivableAmount = 0.00, sterilisationAmount = 0.00;
                res.forEach(function (obj, index) {
                    accountsReceivableAmount = (Number(accountsReceivableAmount) + Number(obj.accountsReceivableAmount)).toFixed(2);
                    sterilisationAmount = (Number(sterilisationAmount) + Number(obj.sterilisationAmount)).toFixed(2);
                });
                $(".accountsReceivableAmount").html(accountsReceivableAmount);
                $(".sterilisationAmount").html(sterilisationAmount);
                $(".qian").html((Number(accountsReceivableAmount) - Number(sterilisationAmount)).toFixed(2));
            },
            tools: {
                detail: function (row) {
                    layuiFunciton.layerOpen({
                        title: "应收核销明细",
                        finder: "form",
                        area: ['80%', '80%'], //自定义宽高
                        areaType: true,
                        content: "detail.html",
                        data: row,
                        end: function () {
                            layuiFunciton.refresh('formVoucher', 'voucherListId');
                        }
                    });
                }, export: function () {
                    location.href = layuiFunciton.URL + '/wb/sterilisation/records/export?' + $("#searchForm").serialize();
                }
            }
        });
    };
    //获取当月第一天
    this.getCurrentMonthFirst = function () {
        var date = new Date();
        date.setDate(1);
        return orderList.dateFtt("yyyy-MM-dd", date);
    };
    //获取当月最后一天
    this.getCurrentMonthLast = function () {
        var date = new Date();
        var currentMonth = date.getMonth();
        var nextMonth = ++currentMonth;
        var nextMonthFirstDay = new Date(date.getFullYear(), nextMonth, 1);
        var oneDay = 1000 * 60 * 60 * 24;
        return orderList.dateFtt("yyyy-MM-dd", new Date(nextMonthFirstDay - oneDay));
    };
    //时间格式化
    this.dateFtt = function (fmt, date) {
        var o = {
            "M+": date.getMonth() + 1,     //月份
            "d+": date.getDate(),     //日
            "h+": date.getHours(),     //小时
            "m+": date.getMinutes(),     //分
            "s+": date.getSeconds(),     //秒
            "q+": Math.floor((date.getMonth() + 3) / 3), //季度
            "S": date.getMilliseconds()    //毫秒
        };
        if (/(y+)/.test(fmt))
            fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
        for (var k in o)
            if (new RegExp("(" + k + ")").test(fmt))
                fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
        return fmt;
    };
    this.event = function () {
        $("#search").click(function () {
            if($("#orderDateStart").val().length == 0 || $("#orderDateEnd").val().length == 0){
                if(!($("#orderDateStart").val().length == 0 && $("#orderDateEnd").val().length == 0)){
                    layuiFunciton.warn('下单日期的开始时间结束时间必填！');
                    return;
                }
            }
            if($("#dateShipmentStart").val().length == 0 || $("#dateShipmentEnd").val().length == 0){
                if(!($("#dateShipmentStart").val().length == 0 && $("#dateShipmentEnd").val().length == 0)){
                    layuiFunciton.warn('发运日期的开始时间结束时间必填！');
                    return;
                }
            }
            layuiFunciton.refresh('searchForm', 'orderListId');
        });
        $("#reload").click(function () {
            $("#orderDateStart").val("");
            $("#orderDateEnd").val("");
            $("#dateShipmentStart").val("");
            $("#dateShipmentEnd").val("");
        });
    };
};
orderList = new OrderList();
orderList.init();
orderList.initTalbe();
orderList.event();
